package com.fenghongzhang.kuaishou.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.fenghongzhang.kuaishou.InitApp;

public class SPUtil {
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;
    private static SPUtil spUtil = null;
    private static final String SP_NAME = "LOGIN";

    private SPUtil(){
        sp = InitApp.getContext.getSharedPreferences(SP_NAME,Context.MODE_PRIVATE);
        editor = sp.edit();
    }


    public static SPUtil getInstance(){
        if(spUtil == null){
            return spUtil = new SPUtil();
        }
        return spUtil;
    }

    //存
    public void putString(String key,String value){
        editor.putString(key,value);
        editor.commit();
    }
    //取
    public String  getString(String key){
        return sp.getString(key,"");
    }


    //根据类型自动存
    public void put(String key, Object o){
        if (o instanceof String){
            editor.putString(key, (String) o);
        }else if(o instanceof Integer){
            editor.putInt(key, (Integer) o);
        }else if(o instanceof Boolean){
            editor.putBoolean(key, (Boolean) o);
        }else if(o instanceof Long){
            editor.putLong(key, (Long) o);
        }else if(o instanceof Float){
            editor.putFloat(key, (Float) o);
        }
    }

    public SharedPreferences getSP(){
        return sp;
    }



    //根据类型自动取
//    public static <T> T get(String key,T type){
//        if (type instanceof String){
//            sp.getString(key, "");
//        }else if(type instanceof Integer){
//            sp.getInt(key, 0);
//        }
//        return null;
//    }



    //清空
    public void clear(){
        editor.clear();
        editor.commit();
    }


}
